/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.controller;

import com.techshop.model.Color;
import com.techshop.model.Phone;
import com.techshop.model.PhoneCode;
import com.techshop.service.ColorService;
import com.techshop.service.PhoneCodeService;
import com.techshop.service.PhoneService;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Hai-PC
 */
@ManagedBean(name = "phoneCodeController")
@SessionScoped
public class PhoneCodeController extends AbstractController
{
    private PhoneCode selectedPhoneCode;
    private List<PhoneCode> phoneCodeList;
    private List<Phone> phoneList;
    private List<Color> colorList;
    private boolean isSearch = false;
    
    @ManagedProperty(value = "#{phoneCodeService}")
    private PhoneCodeService phoneCodeService;
    @ManagedProperty(value = "#{phoneService}")
    private PhoneService phoneService;
    @ManagedProperty(value = "#{colorService}")
    private ColorService colorService;

    public void resetSelectedPhoneCode()
    {
        selectedPhoneCode = new PhoneCode();
        selectedPhoneCode.setColor(new Color());
        selectedPhoneCode.setPhone(new Phone());
    }

    @PostConstruct
    public void init()
    {
        resetSelectedPhoneCode();
    }
    
    public void insert()
    {
        isSearch = false;
        if (phoneCodeService.insert(selectedPhoneCode))
        {
            addInfoMessage("Thêm thành công");
            resetSelectedPhoneCode();
        }
        else
        {
            addErrorMessage("Thêm thất bại");
        }
    }

    public void update()
    {
        isSearch = false;
        if (phoneCodeService.update(selectedPhoneCode))
        {
            addInfoMessage("Cập nhật thành công");
        }
        else
        {
            addErrorMessage("Cập nhật thất bại");
        }
    }
    
    public void delete()
    {
        isSearch = false;
        if (phoneCodeService.delete(selectedPhoneCode))
        {
            addInfoMessage("Xóa thành công");
        }
        else
        {
            addErrorMessage("Xóa thất bại");
        }
    }
    
    public void search()
    {
        isSearch = true;
        setPhoneCodeList(phoneCodeService.search(selectedPhoneCode.getPhone().getName()));
    }
    
    public List<PhoneCode> getPhoneCodeList()
    {
        if (!isSearch)
        {
            setPhoneCodeList(phoneCodeService.getAll());
        }
        return phoneCodeList;
    }

    public void setPhoneCodeList(List<PhoneCode> phoneCodeList)
    {
        this.phoneCodeList = phoneCodeList;
    }

    public PhoneCodeService getPhoneCodeService()
    {
        return phoneCodeService;
    }

    public void setPhoneCodeService(PhoneCodeService phoneCodeService)
    {
        this.phoneCodeService = phoneCodeService;
    }

    public PhoneCode getSelectedPhoneCode()
    {
        return selectedPhoneCode;
    }

    public void setSelectedPhoneCode(PhoneCode selectedPhoneCode)
    {
        this.selectedPhoneCode = selectedPhoneCode;
    }

    public List<Phone> getPhoneList()
    {
        setPhoneList(phoneService.getAll());
        return phoneList;
    }

    public void setPhoneList(List<Phone> phoneList)
    {
        this.phoneList = phoneList;
    }

    public List<Color> getColorList()
    {
        try
        {
            setColorList(colorService.getAllExceptPhoneId(selectedPhoneCode.getPhone().getId()));
        }
        catch (NullPointerException e)
        {
        }
        return colorList;
    }

    public void setColorList(List<Color> colorList)
    {
        this.colorList = colorList;
    }

    public PhoneService getPhoneService()
    {
        return phoneService;
    }

    public void setPhoneService(PhoneService phoneService)
    {
        this.phoneService = phoneService;
    }

    public ColorService getColorService()
    {
        return colorService;
    }

    public void setColorService(ColorService colorService)
    {
        this.colorService = colorService;
    }
}